package com.meiqiongproperty.yiguanjia.fragment.order.orderAdapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.meiqiongproperty.yiguanjia.R;
import com.meiqiongproperty.yiguanjia.databinding.OrderShopListItemBinding;
import com.meiqiongproperty.yiguanjia.fragment.home.adapter.OnItemClickListener;
import com.meiqiongproperty.yiguanjia.httpbean.order.ShopOrderBean;

import java.util.ArrayList;
import java.util.List;

/**
 * author: Administrator
 * date: 2017/5/14.
 * package:com.meiqiongproperty.yiguanjia.fragment.home.adapter.orderAdapter
 */

public class OrderShopListAdapter extends RecyclerView.Adapter<OrderShopListAdapter.BindingHolder> {
    Context context;
    OrderShopListItemBinding binding;
    LayoutInflater inflater;
    private OnItemClickListener mOnItemClickListener;
    List<ShopOrderBean.OrderlistBean> list = new ArrayList<>();

    public OrderShopListAdapter(Context context, List<ShopOrderBean.OrderlistBean> list) {
        this.context = context;
        this.list = list;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public OrderShopListAdapter.BindingHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        binding = DataBindingUtil.inflate(inflater, R.layout.order_shop_list_item, parent, false);
        OrderShopListAdapter.BindingHolder holder = new OrderShopListAdapter.BindingHolder(binding.getRoot());
        holder.setBinding(binding);
        return holder;
    }

    @Override
    public void onBindViewHolder(final OrderShopListAdapter.BindingHolder holder, final int position) {
        ShopOrderBean.OrderlistBean bean = list.get(position);
        holder.binding.orderShopItemNum.setText("共" + bean.getProductCount() + "件  合计：");
        holder.binding.orderShopItemPrice.setText("￥" + bean.getOrderPrice());
        if (bean.getPayStatus().equals("1")){
            holder.binding.orderShopPayState.setText("未支付");
        }else if(bean.getPayStatus().equals("2")){
            holder.binding.orderShopPayState.setText("已支付");
        }

        List<String> list_image = bean.getProductImg();
        switch (list_image.size()) {
            case 0:
                holder.binding.shopImg1.setVisibility(View.GONE);
                holder.binding.shopImg2.setVisibility(View.GONE);
                holder.binding.shopImg3.setVisibility(View.GONE);
                holder.binding.shopImg4.setVisibility(View.GONE);
                holder.binding.imgMore.setVisibility(View.GONE);
                break;
            case 1:
                holder.binding.shopImg1.setVisibility(View.VISIBLE);
                holder.binding.shopImg2.setVisibility(View.GONE);
                holder.binding.shopImg3.setVisibility(View.GONE);
                holder.binding.shopImg4.setVisibility(View.GONE);
                holder.binding.imgMore.setVisibility(View.GONE);
                Glide.with(context).load(list_image.get(0)).into(holder.binding.shopImg1);
                break;
            case 2:
                holder.binding.shopImg1.setVisibility(View.VISIBLE);
                holder.binding.shopImg2.setVisibility(View.VISIBLE);
                holder.binding.shopImg3.setVisibility(View.GONE);
                holder.binding.shopImg4.setVisibility(View.GONE);
                holder.binding.imgMore.setVisibility(View.GONE);
                Glide.with(context).load(list_image.get(0)).into(holder.binding.shopImg1);
                Glide.with(context).load(list_image.get(1)).into(holder.binding.shopImg2);
                break;
            case 3:
                holder.binding.shopImg1.setVisibility(View.VISIBLE);
                holder.binding.shopImg2.setVisibility(View.VISIBLE);
                holder.binding.shopImg3.setVisibility(View.VISIBLE);
                holder.binding.shopImg4.setVisibility(View.GONE);
                holder.binding.imgMore.setVisibility(View.GONE);
                Glide.with(context).load(list_image.get(0)).into(holder.binding.shopImg1);
                Glide.with(context).load(list_image.get(1)).into(holder.binding.shopImg2);
                Glide.with(context).load(list_image.get(2)).into(holder.binding.shopImg3);
                break;
            case 4:
                holder.binding.shopImg1.setVisibility(View.VISIBLE);
                holder.binding.shopImg2.setVisibility(View.VISIBLE);
                holder.binding.shopImg3.setVisibility(View.VISIBLE);
                holder.binding.shopImg4.setVisibility(View.VISIBLE);
                holder.binding.imgMore.setVisibility(View.VISIBLE);
                Glide.with(context).load(list_image.get(0)).into(holder.binding.shopImg1);
                Glide.with(context).load(list_image.get(1)).into(holder.binding.shopImg2);
                Glide.with(context).load(list_image.get(2)).into(holder.binding.shopImg3);
                Glide.with(context).load(list_image.get(3)).into(holder.binding.shopImg4);
                break;
        }


        if (mOnItemClickListener != null) {
            /**
             * 这里加了判断，itemViewHolder.itemView.hasOnClickListeners()
             * 目的是减少对象的创建，如果已经为view设置了click监听事件,就不用重复设置了
             * 不然每次调用onBindViewHolder方法，都会创建一个监听事件对象，增加了内存的开销
             */
            if (!holder.itemView.hasOnClickListeners()) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int position = holder.getPosition();
                        mOnItemClickListener.onItemClick(v, position);
                    }
                });
            }
        }

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class BindingHolder extends RecyclerView.ViewHolder {
        OrderShopListItemBinding binding;

        public OrderShopListItemBinding getBinding() {
            return binding;
        }

        public void setBinding(OrderShopListItemBinding binding) {
            this.binding = binding;
        }

        public BindingHolder(View itemView) {
            super(itemView);
        }
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }
}
